#include "udp_server.hpp"
#include "err.hpp"
#include <memory>
#include <string>
#include <ctype.h>

using namespace Alpaca;
using namespace std;

static void Usage(string proc)
{
    std::cout << "Usage:\n\t" << proc << " port\n"
              << endl;
}

string transactionString(string request)
{
    std::string result;
    char c;
    for (auto &it : request)
    {
        c = it;
        if (islower(it))
            c = toupper(it);
        result.push_back(c);
    }
    return result;
}

bool ispass(std::string command)
{
    auto pos = command.find("rm");
    if(pos == command.npos) return false;
    pos = command.find("mv");
    if(pos == command.npos) return false;

    return true;
}

// 在本地执行远端命令
string excuteCommand(string command)
{
    if (ispass(command))
        return "BAD MAN";

    FILE *fp = popen(command.c_str(), "r");
    if (fp == nullptr)
    {
        return "NONE";
    }

    char line[1024];
    std::string result;
    while (fgets(line, sizeof(line), fp) != NULL)
    {
        result += line;
    }

    pclose(fp);
    return result;
}

int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        Usage(argv[0]);
        exit(USAGE_ERR);
    }

    uint16_t port = atoi(argv[1]);
    unique_ptr<UdpServer> usvr(new UdpServer(port)); // 云服务器不需要自己指明IP

    usvr->start();
    return 0;
}